package com.open4cn.simulator.core.ext;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-01 18:46
 */
public class DataColorStatus implements Serializable {

    public static final String EMPTY_CORRECTION_RECORD = "xxxxxxx";

    // 校准阶段
    private CorrectionStageEnum currentCorrectionStage;

    // 黑板校准次数
    private int blackboardFrequency;

    // 白板校准次数
    private int whiteboardFrequency;

    // 绿板校准次数
    private int greenboardFrequency;

    // 是否已经有了标准绿板值
    private boolean hasGreenStandard;

    // 当前孔径
    private ApertureEnum currentAperture;

    // 当前uv值
    private UVEnum currentUV;

    // 当前包含状态
    private SpecularGlossEnum currentSpecularGloss;

    // 校正历史
    private List<String> correctionHistory;

    public DataColorStatus() {
        init();
    }

    public DataColorStatus(String currentAperture, String currentUV, String currentSpecularGloss) {
        init();
        this.currentAperture = ApertureEnum.valueOf(currentAperture);
        this.currentUV = UVEnum.valueOf(currentUV);
        this.currentSpecularGloss = SpecularGlossEnum.valueOf(currentSpecularGloss);
    }


    /**
     * 重置状态
     */
    public void reset() {
        init();
    }

    public void init() {
        currentCorrectionStage = CorrectionStageEnum.DONE;
        blackboardFrequency = 0;
        whiteboardFrequency = 0;
        greenboardFrequency = 0;
        hasGreenStandard = false;
        currentAperture = ApertureEnum.N;
        currentUV = UVEnum.UV100;
        currentSpecularGloss = SpecularGlossEnum.SCI;
        correctionHistory = new ArrayList<>(
                Arrays.asList(
                        EMPTY_CORRECTION_RECORD,
                        EMPTY_CORRECTION_RECORD,
                        EMPTY_CORRECTION_RECORD,
                        EMPTY_CORRECTION_RECORD,
                        EMPTY_CORRECTION_RECORD,
                        EMPTY_CORRECTION_RECORD,
                        EMPTY_CORRECTION_RECORD,
                        EMPTY_CORRECTION_RECORD)
        );
    }


    /**
     * 开始校准
     */
    public void startCorrection() {
        blackboardFrequency = 0;
        whiteboardFrequency = 0;
        greenboardFrequency = 0;
        currentCorrectionStage = CorrectionStageEnum.PRE;
    }

    /**
     * 校准结束
     */
    public void stopCorrection() {
        blackboardFrequency = 0;
        whiteboardFrequency = 0;
        greenboardFrequency = 0;
        currentCorrectionStage = CorrectionStageEnum.DONE;
    }


    /**
     * 触发黑板校准
     */
    public void triggerBlackboard() {
        blackboardFrequency++;
    }

    /**
     * 触发白板校准
     */
    public void triggerWhiteboard() {
        whiteboardFrequency++;
    }

    /**
     * 触发绿板校准
     */
    public void triggerGreenboard() {
        greenboardFrequency++;
    }

    public boolean inCorrecting() {
        return !CorrectionStageEnum.DONE.equals(currentCorrectionStage);
    }

    public SpecularGlossEnum getCurrentSpecularGloss() {
        return currentSpecularGloss;
    }

    /**
     * 切换镜面光泽包含值
     *
     * @param specularGloss
     */
    public void changeSpecularGloss(SpecularGlossEnum specularGloss) {
        this.currentSpecularGloss = specularGloss;
    }

    public ApertureEnum getCurrentAperture() {
        return currentAperture;
    }

    /**
     * 切换孔径
     *
     * @param aperture
     */
    public void changeAperture(ApertureEnum aperture) {
        this.currentAperture = aperture;
    }

    public UVEnum getCurrentUV() {
        return currentUV;
    }

    /**
     * 切换uv值
     *
     * @param uv
     */
    public void changeUV(UVEnum uv) {
        this.currentUV = uv;
    }

    public List<String> getCorrectionHistory() {
        return correctionHistory;
    }

    /**
     * 更改校准历史
     *
     * @param slot
     * @param history
     */
    public void changeCorrectionHistory(int slot, String history) {
        if (0 <= slot && slot <= 7) {
            correctionHistory.set(slot, history);
        }
    }

    public CorrectionStageEnum getCurrentCorrectionStage() {
        return currentCorrectionStage;
    }

    public void changeCorrectionStage(CorrectionStageEnum correctionStage) {
        this.currentCorrectionStage = correctionStage;
    }

    public int whiteboardFrequency() {
        return whiteboardFrequency;
    }

    public int greenboardFrequency() {
        return greenboardFrequency;
    }

    public boolean hasGreenStandard() {
        return hasGreenStandard;
    }

    public void setHasGreenStandard(boolean hasGreenStandard) {
        this.hasGreenStandard = hasGreenStandard;
    }

    public int getBlackboardFrequency() {
        return blackboardFrequency;
    }

    @Override
    public String toString() {
        return "DataColorStatus{" +
                "currentCorrectionStage=" + currentCorrectionStage +
                ", blackboardFrequency=" + blackboardFrequency +
                ", whiteboardFrequency=" + whiteboardFrequency +
                ", greenboardFrequency=" + greenboardFrequency +
                ", hasGreenStandard=" + hasGreenStandard +
                ", currentAperture=" + currentAperture.name() +
                ", currentUV=" + currentUV.name() +
                ", currentSpecularGloss=" + currentSpecularGloss.name() +
                ", correctionHistory=" + correctionHistory +
                '}';
    }

    /**
     * --------------------------------------------------------------------
     **/

    // 孔径枚举
    public enum ApertureEnum {
        N,
        M,
        S,
        U,
        A;

        public static ApertureEnum of(String code) {
            for (ApertureEnum item : ApertureEnum.values()) {
                if (item.name().equals(code)) {
                    return item;
                }
            }
            return null;
        }
    }


    // uv枚举
    public enum UVEnum {
        UV100("000"),
        UV0("001"),
        ;
        private String code;

        UVEnum(String code) {
            this.code = code;
        }

        public String getCode() {
            return code;
        }

        public static UVEnum of(String code) {
            for (UVEnum item : UVEnum.values()) {
                if (item.code.equals(code)) {
                    return item;
                }
            }
            return null;
        }
    }

    // 包含状态枚举
    public enum SpecularGlossEnum {
        SCI("I"),
        SCE("E");
        private String code;

        SpecularGlossEnum(String code) {
            this.code = code;
        }

        public String getCode() {
            return code;
        }

        public static SpecularGlossEnum of(String code) {
            for (SpecularGlossEnum item : SpecularGlossEnum.values()) {
                if (item.code.equals(code)) {
                    return item;
                }
            }
            return null;
        }
    }

    // 校准阶段
    public enum CorrectionStageEnum {
        PRE,
        BLACK_BOARD,
        WHITE_BOARD,
        WHITE_BOARD_END,
        GREEN_BOARD,
        DONE,
        ;
    }

}
